package com.hanrui.android.net.presenter.message.audit.askforleave;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.AskForLeaveDetailBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.audit.askforleave.AskForLeaveDetailContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 请假
 */
public class AskForLeaveDetailPresenter extends BasePresenter<AskForLeaveDetailContract.View> implements AskForLeaveDetailContract.Presenter{
    @Inject
    public AskForLeaveDetailPresenter(){}

    @Override
    public void askForLeaveBizDetail(final Context context, String taskId, String procInsId, String bizId) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .askForLeaveBizDetail(taskId,procInsId,bizId)
                .compose(RxSchedulers.<BaseBean<AskForLeaveDetailBean>>applySchedulers())
                .compose(mView.<BaseBean<AskForLeaveDetailBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<AskForLeaveDetailBean>>() {
                    @Override
                    public void accept(BaseBean<AskForLeaveDetailBean> response) throws Exception {
                        Log.e(Constant.tag,response.getCode()+"");
                        mView.hideLoading();
                        if (response.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else if (response.getCode() == 1) {
                            mView.askForLeaveBizDetail(response.getData());
                        } else {
                            mView.showFaild(String.valueOf(response.getMessage()));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.showFaild(throwable.getMessage());
                    }
                });


    }
}
